package com.azure.service.Report;

import com.azure.entity.AProductDailyReport;
import com.azure.entity.AProductMonthlyhotReport;
import com.azure.entity.AProductProvinceReport;
import com.azure.mapper.bigstore.TCategoryMapper;
import com.azure.mapper.bigstore.TOrderitemMapper;
import com.azure.mapper.bigstore.TProductMapper;
import com.azure.mapper.report.AProductDailyReportMapper;
import com.azure.mapper.report.AProductMonthlyhotReportMapper;
import com.azure.mapper.report.AProductProvinceReportMapper;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhuaibin on 2018/3/13.
 */
@Service
public class ProductReportService {
    @Autowired
    private TProductMapper tProductMapper;
    @Autowired
    private TOrderitemMapper tOrderitemMapper;
    @Autowired
    private AProductDailyReportMapper aProductDailyReportMapper;
    @Autowired
    private AProductMonthlyhotReportMapper aProductMonthlyhotReportMapper;
    @Autowired
    private AProductProvinceReportMapper aProductProvinceReportMapper;

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMM");
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMM");

    public void totalDailyReport(LocalDate date){
        //pid--hot
        List<JSONObject> jsonObjects = tOrderitemMapper.totalDailyReport(date);
        if(jsonObjects==null || jsonObjects.size() == 0){
            System.out.println("=== ProductReportService totalDailyReport  !!!empty list!!! tOrderitemMapper.totalDailyReport() ===");
            return;
        }
        Map<Long,Integer> pid_hot_map = new HashMap<>();
        List<Long> pids = new ArrayList<>();
        for(JSONObject object : jsonObjects){
            pid_hot_map.put(Long.valueOf(object.getAsString("pid")), Integer.valueOf(object.getAsString("hot")));
            pids.add(Long.valueOf(object.getAsString("pid")));
        }
        //pid--ptitle
        List<JSONObject> pidCategoryList = tProductMapper.selectByPids(pids);
        Map<Long, String> pid_ptitle_map = new HashMap<>();
        for(JSONObject object : pidCategoryList){
            pid_ptitle_map.put(Long.valueOf(object.getAsString("id")), object.getAsString("title") );
        }

        Map<String,Integer> ptitle_phot_map = new HashMap<>();
        for(Long pid : pid_hot_map.keySet()){
            ptitle_phot_map.put( pid_ptitle_map.get(pid) , pid_hot_map.get(pid));
            aProductDailyReportMapper.insert(new AProductDailyReport(dateTimeFormatter.format(date),
                                        pid_ptitle_map.get(pid), Long.valueOf(pid_hot_map.get(pid).toString()) ) );
        }
        System.out.println("====totalDailyReport  ptitle_phot_map :"+ptitle_phot_map);
    }

    public void totalMonthlyReport(LocalDate startDate, LocalDate stopDate){
        //pid--hot
        List<JSONObject> jsonObjects = tOrderitemMapper.totalMonthlyReport(startDate,stopDate);
        if(jsonObjects==null || jsonObjects.size() == 0){
            System.out.println("=== ProductReportService totalDailyReport  !!!empty list!!! tOrderitemMapper.totalDailyReport() ===");
            return;
        }
        Map<Long,Integer> pid_hot_map = new HashMap<>();
        List<Long> pids = new ArrayList<>();
        for(JSONObject object : jsonObjects){
            pid_hot_map.put(Long.valueOf(object.getAsString("pid")), Integer.valueOf(object.getAsString("hot")));
            pids.add(Long.valueOf(object.getAsString("pid")));
        }
        //pid--ptitle
        List<JSONObject> pidCategoryList = tProductMapper.selectByPids(pids);
        Map<Long, String> pid_ptitle_map = new HashMap<>();
        for(JSONObject object : pidCategoryList){
            pid_ptitle_map.put(Long.valueOf(object.getAsString("id")), object.getAsString("title") );
        }

        Map<String,Integer> ptitle_phot_map = new HashMap<>();
        for(Long pid : pid_hot_map.keySet()){
            ptitle_phot_map.put( pid_ptitle_map.get(pid) , pid_hot_map.get(pid));
            aProductMonthlyhotReportMapper.insert(new AProductMonthlyhotReport(dateTimeFormatter2.format(startDate),
                    pid_ptitle_map.get(pid), Long.valueOf(pid_hot_map.get(pid).toString()) ) );
        }
        System.out.println("====totalMonthlyReport  ptitle_phot_map :"+ptitle_phot_map);
    }


    public Map<String,Object> getDailyProductData(String date){
        Map<String,Object> map = new HashMap<>();
        List<String> productNames = new ArrayList<>();
        List<Long> productHot = new ArrayList<>();

        List<AProductDailyReport> dailyProductData = aProductDailyReportMapper.getDailyProductData(date);
        if(dailyProductData==null || dailyProductData.size()==0){
            System.out.println("=== ProductReportService getDailyProductData  !!!empty list!!! aProductDailyReportMapper.getDailyProductData() ===");
            return map;
        }
        System.out.println("=== ProductReportService dailyProductData: "+dailyProductData);
        for(AProductDailyReport aProductDailyReport : dailyProductData){
            productNames.add("\""+aProductDailyReport.getProduct()+"\"");
            productHot.add(aProductDailyReport.getHot());
        }
        map.put("productNames",productNames);
        map.put("productHot",productHot);
        map.put("dailyDate","\""+date+"日数据\"");
        return map;
    }
    public Map<String,Object> getMonthlyProductData(String date){
        Map<String,Object> map = new HashMap<>();
        List<String> productNames = new ArrayList<>();
        List<Long> productHot = new ArrayList<>();

        List<AProductMonthlyhotReport> monthlyProductData = aProductMonthlyhotReportMapper.getMonthlyProductData(date);
        if(monthlyProductData==null || monthlyProductData.size()==0){
            System.out.println("=== ProductReportService getMonthlyProductData  !!!empty list!!! aProductMonthlyhotReportMapper.getMonthlyProductData() ===");
            return map;
        }
        System.out.println("=== ProductReportService monthlyProductData: "+monthlyProductData);
        for(AProductMonthlyhotReport aProductMonthlyhotReport : monthlyProductData){
            productNames.add("\""+aProductMonthlyhotReport.getProduct()+"\"");
            productHot.add(aProductMonthlyhotReport.getHot());
        }
        map.put("productNames",productNames);
        map.put("productHot",productHot);
        map.put("monthlyDate","\""+date+"月数据\"");
        return map;
    }
}
